package edu.zju.gis.dbfg.fileserver.model;

import edu.zju.gis.dbfg.fileserver.util.DateUtil;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Getter
@Setter
@NoArgsConstructor
@ToString
public class ConstructedDataset2 implements Jsonable, Serializable {
    private String id;

    private String userid;

    private String name;

    private Integer size;

    private String description;

    private String storepath;

    private String picurl;

    private ArrayList<ConstructedDataField2> fields;

    private Date createtime;

    private Date lastmodifytime;

    private Boolean isspatialdata;

    private String datatype;

    private String storetype;

    private String tileServiceId;

    private String source;

    private  String cors;

    public ConstructedDataset2(List<ConstructedDataField2> fields) {
        this.setId(UUID.randomUUID().toString());
        this.userid = "";
        this.name = "DATASET_NAME";
        this.size = -1;
        this.description = "数据描述...";
        this.picurl = "static/AddressMatch.png";
        this.fields = new ArrayList<>();
        this.fields.addAll(fields);
        this.createtime = DateUtil.now();
        this.lastmodifytime = this.createtime;
        this.isspatialdata = false;
        this.datatype = "Table";
        this.storetype = "ES";
        this.cors="4326";
    }

    public ConstructedDataset2(String userId, ArrayList<ConstructedDataField2> fields) {
        this.userid = userId;
        this.id = UUID.randomUUID().toString();
        this.name = "DATASET_NAME";
        this.size = -1;
        this.description = "数据描述...";
        this.picurl = "static/AddressMatch.png";
        this.fields = fields;
        this.createtime = DateUtil.now();
        this.lastmodifytime = this.createtime;
        this.isspatialdata = false;
        this.datatype = "Table";
        this.storetype = "ES";
    }

    public ConstructedDataset2(JSONObject req) throws ParseException {
        this.userid = req.getString("userid");
        this.id = req.getString("id");
        this.name = req.getString("name");
        this.size = req.getInt("size");
        this.description = req.getString("description");
        this.picurl = req.getString("picurl");
        this.createtime = DateUtil.parse(req.getString("createtime"));
        this.lastmodifytime = DateUtil.parse(req.getString("lastmodifytime"));
        this.isspatialdata = req.getBoolean("isspatialdata");
        this.datatype = req.getString("datatype");
        this.storetype = req.getString("storetype");
        this.source = req.getString("source");
        JSONArray fieldsArray = req.getJSONArray("fields");
        ArrayList<ConstructedDataField2> fieldsList = new ArrayList<>();
        for (int i = 0; i < fieldsArray.length(); i++) {
            fieldsList.add(new ConstructedDataField2(fieldsArray.getJSONObject(i)));
        }
        this.fields = fieldsList;
        this.cors=req.getString("cors");
    }

    //    @Override
////    public String id() {
////        return this.id;
////    }
    @Override
    public String getPk_id() {
        return this.getId();
    }

    public ConstructedDataField2 getKeyField() {

        for (ConstructedDataField2 f : fields) {
            if (f.getIsKey()) return f;
        }

        return null;
    }
}
